/**
 * Custom ColumnControlButton for the Item Table (contains the hide function)
 * 
 * @author remo.lemma@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui.table.item;

import java.util.List;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;

import javax.swing.AbstractAction;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JSplitPane;
import javax.swing.table.TableColumn;

import org.jdesktop.swingx.table.ColumnControlButton;
import org.jdesktop.swingx.table.ColumnControlPopup;

import ch.unisi.inf.projects.java.uom.gui.table.UOMItemJTable;
import ch.unisi.inf.projects.java.uom.Constants;

public class UOMItemColumnControlButton extends ColumnControlButton
{
	private static final long serialVersionUID = -1L;
	
	private static final Dimension hiddenDimension = new Dimension(1500,15);
	
	private boolean state;
	
	private final Dimension normalDimension;
	
	private final UOMItemJTable table;

	private AbstractAction hideAction;
	
	private JCheckBoxMenuItem menuItem;
	
	public UOMItemColumnControlButton (final UOMItemJTable table)
	{
		super(table);
		this.table = table;
		hideAction = null;
		normalDimension = new Dimension((int)table.getPreferredScrollableViewportSize().getWidth(),50);
		state = table.isVisible();
		createVisibilityActions();
	}
	
    protected void createVisibilityActions() {
    	if (table == null)
    		return;
        final List<TableColumn> columns = table.getColumns(true);
        for (TableColumn column : columns) {
        	if (column.getIdentifier().equals(Constants.ITEM_PRIMARY_KEY.value())) // Don't add UOMID as column in the control
        		continue;
            ColumnVisibilityAction action = createColumnVisibilityAction(column);
            if (action != null) {
                getColumnVisibilityActions().add(action);
            }
        }

    }
	
	protected void addVisibilityActionItems ()
	{
		super.addVisibilityActionItems();
		final UOMItemColumnControlPopup popup = (UOMItemColumnControlPopup) this.getColumnControlPopup();
		hideAction = new AbstractAction("Hide") {
			private static final long serialVersionUID = -1L;
			public void actionPerformed (ActionEvent e) { 
				state = !state; 
				final Component scrollPane = table.getParent().getParent();
				final Component panel = scrollPane.getParent();
				final JSplitPane p = (JSplitPane) panel.getParent();
				Dimension dimension;
				if (state) {
					dimension = normalDimension;
					table.toShow();
				}
				else {
					dimension = hiddenDimension;
					table.toHide();

				}
				resizeNow(table, dimension);
				resizeNow(table.getParent(), dimension);
				resizeNow(scrollPane, dimension);
				if (state) {
			        scrollPane.setPreferredSize(new Dimension((int) normalDimension.getWidth(),1500));
			        scrollPane.setMaximumSize(new Dimension(Integer.MAX_VALUE,1500));
			        table.setMinimumSize(new Dimension(0,0));
			        scrollPane.setMinimumSize(new Dimension(0,0));
			        table.getParent().setMinimumSize(new Dimension(0,0));
			        table.setMaximumSize(new Dimension(Integer.MAX_VALUE,1500));
				}
				resizeNow(panel, dimension);
				if (state)
					p.setDividerLocation(0.6);
				else
					p.setDividerLocation(0.979);
				
				p.setEnabled(state);
				p.repaint();
			} };
			menuItem = new JCheckBoxMenuItem(hideAction);
			popup.addFunction(menuItem,getVisibility());
	}
	
	public void hide()
	{
		if (hideAction != null && getVisibility() == true) {
			hideAction.actionPerformed(null);
			state = !state;
			menuItem.setSelected(state);
			state = !state;
		}
	}
	
	private boolean getVisibility ()
	{
		if (table == null)
			return false;
		else
			return !table.isVisible();
	}
	
	private void resizeNow (final Component component, final Dimension dimension)
	{
		component.setMaximumSize(dimension);
		component.setMinimumSize(dimension);
		component.setPreferredSize(dimension);
	}
	
	protected ColumnVisibilityAction createColumnVisibilityAction(TableColumn column) {
        return new UOMColumnVisibilityAction(column);
    }
	
	protected ColumnControlPopup createColumnControlPopup ()
	{
		return new UOMItemColumnControlPopup();
	}
    
    class UOMItemColumnControlPopup extends ColumnControlButton.DefaultColumnControlPopup
    {
    	
    	public UOMItemColumnControlPopup ()
    	{
    		super();
    	}
    	
    	public void addFunction (final JCheckBoxMenuItem item, final boolean value)
    	{
    		this.addSeparator();
    		item.setSelected(value);
    		addItem(item);
    	}
    }
    
    class UOMColumnVisibilityAction extends ColumnVisibilityAction
    {
    	/** We are not going to serialize this class atm */
        private static final long serialVersionUID = -1L;
    	
    	final TableColumn column;
    	
    	public UOMColumnVisibilityAction (final TableColumn column)
    	{
    		super(column);
    		this.column = column;
    	}
    	
    	public void itemStateChanged (final ItemEvent e)
    	{
    		super.itemStateChanged(e);
    		if (canControlColumn() && (e.getStateChange() == ItemEvent.DESELECTED) && (table.getColumnCount() > 1)) {
    			final TableColumn t = table.getSortedColumn();
    			if (t == null || this.column.equals(t)) {
    				table.toggleSortOrder(0);
    			}
    		}
    	}
    }
}

